package i_quartz;

import java.util.Date;
import java.text.SimpleDateFormat;

import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RAMQuartz {
	
	private static Logger logger=LoggerFactory.getLogger(RAMQuartz.class);
	
	public static void main(String[] args) throws SchedulerException {
		//1.创建Scheduler的工厂
		StdSchedulerFactory sf = new StdSchedulerFactory();
		//2.从工厂中获取调度器实例
		Scheduler scheduler = sf.getScheduler();
		
		//3.创建JobDetail
		JobDetail job = JobBuilder.newJob(RAMJob.class)
						.withDescription("这是一个RAMJob")	 	//job的描述
						.withIdentity("ramJob","ramGroup")	//job的name和group
						.build();
		
		//任务运行的时间,SimpleSchedle类型触发器有效
		long time=System.currentTimeMillis()+3*1000L;	//3秒后启动任务
		Date startTime = new Date(time);
		
		//4.创建Trigger
		//使用SimpleScheduleBuilder或者 CronScheduleBuilder
		Trigger t = TriggerBuilder.newTrigger()
							.withDescription("")
							.withIdentity("ramTrigger","ramTriggerGroup")
							.startAt(startTime)			//默认当前时间启动
							.withSchedule(CronScheduleBuilder.cronSchedule("0/2 * * * * ?")) 	//两秒执行
							.build();
		
		//5.注册任务和定时器
		scheduler.scheduleJob(job,t);
		
		//6.启动调度器
		scheduler.start();
		logger.info("启动时间:"+new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
		
	}
	
	
}
